package com.changgou.seckill.service.impl;

import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.service.SecKillGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author LQQ
 * @Date 2020/3/18 15:58
 * @Version 1.0
 */
@Service
public class SecKillGoodsServiceImpl implements SecKillGoodsService {


   @Autowired
   private RedisTemplate redisTemplate;

    public static final String SECKILL_GOODS_STOCK_COUNT_KEY = "seckill_goods_stock_count_";

    public static final String SECKILL_GOODS_KEY = "seckill_goods_";

    /**
     * 根据秒杀时间段查询Redis中的商品信息
     * @param time
     * @return
     */
    public List<SeckillGoods> list(String time) {

        //取redis中的值
        List<SeckillGoods> list = redisTemplate.boundHashOps(SECKILL_GOODS_KEY + time).values();

        //更新库存数据的来源
        for (SeckillGoods seckillGoods : list) {
            String value = (String) redisTemplate.opsForValue().get(SECKILL_GOODS_STOCK_COUNT_KEY + seckillGoods.getId());

                seckillGoods.setStockCount(Integer.parseInt(value));
        }

        return list;
    }
}
